package local.blueheart.data.jxmobilemigrate.domino;

import local.blueheart.data.jxmobilemigrate.util.NotesUtil;
import lotus.domino.Database;
import lotus.domino.NotesException;
import lotus.domino.View;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Enumeration;
import java.util.Vector;

public class DominoDb {

    private static final Logger LOGGER = LoggerFactory.getLogger(DominoDb.class);

    private Database db;

    private Vector<DominoView> views;

    public DominoDb(Database db) {
        this.db = db;
        views = new Vector<>();
    }

    public DominoView getViewByName(String name){
        DominoView dominoView =null;
        View view = null;
        try {
            view = db.getView(name);
            //加入到子view池，recycle释放不会漏掉
            views.addElement(new DominoView(view));
        } catch (NotesException e) {
             LOGGER.error("get view failure",e);
        }
        return null;

    }

    public boolean recycle(){
        //recycle掉所有的view
        Enumeration<DominoView> elements = views.elements();
        while (elements.hasMoreElements()){
            DominoView dominoView = elements.nextElement();
            dominoView.recycle();
        }
        //recycle掉自己的db
        return NotesUtil.recycle(db);
        //return true;
    }
}
